import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UsersService,
    private readonly jwtService: JwtService,
  ) { }
  async createToken(name: string, password: string) {
    // if (name !== password) {
    //   throw new UnauthorizedException();
    // }

    const user = { name };
    const expiration = 60 * 60 * 60;
    // 将使用者资讯加密
    const accessToken = this.jwtService.sign(user, {
      // 关于建立token时相关参数
      // 过期时间
      expiresIn: expiration,
    });
    return accessToken

  }
  async validateUser(payload) {
    return await this.userService.findOneByName(payload);
  }
}
